package com.hb.trade.api.routine.controller.flow;

import cn.hutool.core.util.StrUtil;
import com.hb.core.pojo.flow.dto.FlowTempFormDTO;
import com.hb.core.pojo.flow.entity.FlowTempFormField;
import com.hb.trade.client.flow.FlowClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.fzzn.core.auth.shiro.util.ShiroUtil;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.model.enums.MsgEnum;
import org.fzzn.core.model.msg.ResponseDto;
import org.fzzn.core.model.user.login.LoginUser;
import org.fzzn.core.util.ResultUtil;
import org.fzzn.core.web.anno.Version;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@Slf4j
@Version(1.0)
@Api(tags = {"工作流 - 表单设计"})
@RequestMapping("/{version}")
public class ApiFlowTempFormController {

    protected final FlowClient flowClient;

    public ApiFlowTempFormController(FlowClient flowClient) {
        this.flowClient = flowClient;
    }

    @ApiOperation(value = "获取所有表单信息", notes = "获取所有表单信息", httpMethod = "POST")
    @PostMapping("/flow/temp/forms")
    public ResponseDto tempForms() {
        Message message = flowClient.flowTemps();
        return ResultUtil.msg(message);
    }

    @ApiOperation(value = "获取指定表单的字段信息", notes = "获取指定表单的字段信息", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "formId", value = "表单信息ID", dataTypeClass = Long.class, required = true)
    })
    @PostMapping("/flow/temp/form/fields")
    public ResponseDto tempFormFields(@ApiIgnore @RequestParam FlowTempFormField field) {
        Long formId = field.getFormId();
        if (formId == null) {
            return ResultUtil.msg(MsgEnum.INCORRECT_PARAM);
        }
        Message message = flowClient.tempFormFields(formId);
        return ResultUtil.msg(message);
    }

    @ApiOperation(value = "新增表单信息", notes = "新增表单信息", httpMethod = "POST")
    @PostMapping("/flow/temp/newForm")
    public ResponseDto newForm(@ApiIgnore @RequestParam FlowTempFormDTO formDto) {
        String valid = formDto.paramsValid();
        if (StrUtil.isNotEmpty(valid)) {
            return ResultUtil.fail(valid);
        }
        LoginUser<Long> currentUser = ShiroUtil.getUserEntity();
        formDto.buildBaseData(1, currentUser.getUsername(), currentUser.getId());
        Message message = flowClient.newForm(formDto);
        return ResultUtil.msg(message);
    }
}
